<?php
	namespace Profile\Controller;
	use Think\Controller;
	class BaseController extends Controller {
		private $model;
		private $tbl;
		private $perpage=10;
		public function _initialize(){
			  if(!session('?user')){
				 //var_dump(session('user'));exit;
				 // echo "<meta http-equiv='refresh' content='1;url=index.php/Index/index'> ";
				 //$this->header('__ROOT__/index.php/Index/index');
				 echo "不好意思，没登陆不可以访问这里哦！";
				 exit;
			  }
		}
		public function index($tbl,$ay){
			$this->model=D($tbl);
			$count=$this->model->count();// 查询满足要求的总记录数
			$Page = new \Think\Page($count,$this->perpage);// 实例化分页类
			$show=$Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			
			if($ay){
				$list = $this->model->order("yjl_id desc")->where($ay)->limit($Page->firstRow.','.$Page->listRows)->select();
				
			//var_dump($ay);exit;
			}else{
				$list = $this->model->order("yjl_id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
			}
			
			$this->assign('list',$list);// 赋值数据集
			$this->assign('page',$show);// 赋值分页输出

			// $data=$this->model->select();
			// $this->assign('list',$data);
			$this->display();
		}
		public function form($tbl){
			$this->display();
		}
		public function mod($tbl,$myid){
			//var_dump($tbl);
			$this->model=D($tbl);
			$id=I('get.id');
			if($id==null){
				$id=$_SESSION['user']['yjl_id'];
				//var_dump($id);exit;
			}
			$data=$this->model->where("$myid='{$id}'")->find();
			$this->assign('one',$data);
			$this->display();
		}
		public function update($tbl,$myfilefield){
			$this->model=M($tbl);
			$data=I('post.');
			
			if($_FILES[$myfilefield]['size']!=0){
				//echo $myfilefield;
				$yphoto="y".$myfilefield;
				//echo $data[$yphoto]; 
				if($data[$yphoto]!="00.jpg"){
					$path="./public/photo/".$data[$yphoto];//原图
					$ypath=str_replace("thumb_","",$path);
					if(is_file($path)){//如果存在则删除
						unlink($path);
					}
					if(is_file($ypath)){
							unlink($ypath);
					}
				}
				$data[$myfilefield]=$this->uploadfile($myfilefield);
			}
			$user=$this->model->create($data);
			//var_dump($data);exit;
			$position="index";
			
			if($user){
				$this->model->save();
				
				$this->success('更新成功！',U($position));
			}else{
				$this->error("更新失败！",U($position));
			}
		}
		public function add($tbl,$myfilefield='yjl_photo',$ext){
			$this->model=D($tbl);
			$data=I('post.');
			if($_FILES[$myfilefield]['name']){
				if($ext==1){
					$data[$myfilefield]=$this->uploadWfile($myfilefield);
				}else{
					$data[$myfilefield]=$this->uploadfile($myfilefield);
				}				
			}else{
				$data[$myfilefield]="00.jpg";				
			}	
			if($this->model->create($data)){
				$this->model->add();
				$position="index";
				//echo $position;
				$this->success('增加成功！',U($position));
			}else{
				$this->error($this->model->getError());
				//echo $position;
				$this->error("增加失败！",U($position));
			}
		}
		public function search($tbl,$field){//搜索
			$key=I('post.searchname');
			$sql="select * from yjl_{$tbl} where {$field}   like '%".$key."%'";
			//echo $sql;
			$data=M()->query($sql);
			$this->assign('list',$data);
			$this->display();	
	}
		public function delete($tbl,$idfield='yjl_id',$myfilefield='yjl_photo'){
			$this->model=D($tbl);
			$id=I('get.id');//内容，默认值	
			$data=$this->model->where("$idfield='{$id}'")->find();
			$position="index";
			$result=$this->model->delete($id);
			if($result){
				if($data[$myfilefield]!="00.jpg"){
					$path="./public/photo/".$data[$myfilefield];//原图
					$ypath=str_replace("thumb_","",$path);						
					if(is_file($path)){//如果存在则删除
					if(is_file($ypath)){
						unlink($ypath);
						}
						unlink($path);
					}
				}			
				$this->success('删除成功',U($position));//地址，内容				
			}else{
				$this->error('删除失败',U($position));//
			}
				
		}
		
		public function pass($tbl){//通过审核
			$this->model=M($tbl);
			$data=I('get.');
			// echo $tbl;
			// var_dump($data);exit;
			$user=$this->model->create($data);
			$position="index";
			if($user){
				$this->model->save();
				
				$this->success('更新成功！',U($position));
			}else{
				$this->error("更新失败！",U($position));
			}
		}
		protected function uploadfile($field){
			$config = array(
			'maxSize'    =>    3145728,
			'savePath'   =>    '',
			'saveName'   =>    array('uniqid',''),
			'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
			'autoSub'    =>    true,
			'subName'    =>    array('date','Ymd'),
			);

			$upload = new \Think\Upload($config);// 实例化上传类
			$upload->rootPath = './Public/photo/';
			$upload->saveName = time().'_'.mt_rand();
			// 上传文件 
			$info   =   $upload->uploadOne($_FILES[$field]);
			//dump($info);
			if(!$info) {// 上传错误提示错误信息
				$this->error($upload->getError());
			}else{// 上传成功
				$image = new \Think\Image();
				$tsize=60;
				$position=$upload->rootPath.$info['savepath'];
				//echo $position;
				$image->open($position.$info['savename']);
				$image->thumb($tsize,$tsize,3);
				//echo $position."thumb_".$info['savename'];
				$image->save($position."thumb_".$info['savename']);
				return $info['savepath']."thumb_".$info['savename'];
				
			}
		}
		protected function uploadWfile($field){
			$config = array(
			'maxSize'    =>    3145728,
			'savePath'   =>    '',
			'saveName'   =>    array('uniqid',''),
			'autoSub'    =>    true,
			'subName'    =>    array('date','Ymd'),
			);
			$upload = new \Think\Upload($config);// 实例化上传类
			$upload->rootPath = './Public/file/';
			$upload->saveName = time().'_'.mt_rand();
			// 上传文件 
			$info   =   $upload->uploadOne($_FILES[$field]);
			//dump($info);
			if(!$info) {// 上传错误提示错误信息
				$this->error($upload->getError());
			}else{// 上传成功
				return $info['savepath'].$info['savename'];
			}
		}
	}

?>